<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>
    <script src="../admin/libs/jquery.twbsPagination.js"></script>

</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章列表
        </div>
        <div class="container-fluid common_con">
            <div class="row opt_btns">
                <div class="col-xs-6">
                    <form class="form-inline">
                        <select id="selCategory" name="" class="form-control input-sm">
                            <!-- <option value="">所有分类</option>
                            <option>未分类</option>
                            <option>奇趣事</option>
                            <option>会生活</option>
                            <option>爱旅行</option> -->
                        </select>
                        <select id="selStatus" name="" class="form-control input-sm">
                            <option value="">所有状态</option>
                            <option value="草稿">草稿</option>
                            <option value="已发布">已发布</option>
                        </select>
                        <button id="btnSearch" class="btn btn-default btn-sm">筛选</button>
                    </form>
                </div>
                <div class="col-xs-6">
                    <a href="article_release.html" class="btn btn-success btn-sm pull-right" id="release_btn">发表文章</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>分类</th>
                        <th class="text-center">发表时间</th>
                        <th class="text-center">状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- <tr>
                        <td>王积千造统最头</td>
                        <td>杰克</td>
                        <td>奇趣事</td>
                        <td class="text-center">2017-06-08 07:08:46</td>
                        <td class="text-center">已发布</td>
                        <td class="text-center">
                            <a href="article_edit.html" class="btn btn-default btn-xs">编辑</a>
                            <a href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a>
                        </td>
                    </tr> -->
                </tbody>
            </table>
            <div class="row text-center">
                <ul id="pagination" class="pagination-sm"></ul>
            </div>
        </div>
    </div>
</body>
<script src="./libs/https.js"></script>
<script type="text/html" id='tpl-list'>
{{each data.data}}
<tr>
    <td>{{$value.title}}</td>
    <td>{{$value.author}}</td>
    <td>{{$value.category}}</td>
    <td class="text-center">{{$value.date}}</td>
    <td class="text-center">{{$value.state}}</td>
    <td class="text-center">
        <a href="article_edit.html?id={{$value.id}}" class="btn btn-default btn-xs btn-edit" data-id="{{$value.id}}">编辑</a>
        <a href="javascript:void(0);" class="btn btn-danger btn-xs delete" data-id="{{$value.id}}">删除</a>
    </td>
</tr>

{{/each}}
</script>
<script>
    $(function () {
        $.ajax({
            url: BigNew.category_list,
            success: function (data) {
                // console.log(data);
                let htmlStr = '<option value="">所有分类</option>'
                $.each(data.data, function (index, value) {
                    htmlStr += `<option value='${value.id}'>${value.name}</option>`
                    $('#selCategory').html(htmlStr)
                })
            }
        })
        // 定义当前页数
        let selPage = 1
        // 渲染文章
        function loadData(page) {


            $.ajax({
                url: BigNew.article_query,
                data: {
                    type: $('#selCategory').val(),
                    state: $('#selStatus').val(),
                    page: page,
                    perpage: 5
                },
                success: function (data) {
                    console.log(data);
                    if (data.code == 200) {
                        // 判断有没有数据
                        if (data.data.totalCount > 0) {
                            let list = template('tpl-list', data)
                            $('tbody').html(list)
                            // 设置分页

                            $('#pagination').twbsPagination('destroy');
                            $('#pagination').twbsPagination({
                                totalPages: data.data.totalPage,
                                visiblePages: 10,
                                startPage: page,
                                first: '首页',
                                prev: '上一页',
                                next: '下一页',
                                last: '尾页',
                                // 关闭插件初次自动点击的效果,避免后期陷入递去归来
                                initiateStartPageClick: false,
                                onPageClick: function (event, page) {
                                    console.log(page)
                                    // 把当前页数弄到全局
                                    selPage = page
                                    // page: 是当前被点击的页码
                                    // 请求当前被点击页码的数据
                                    loadData(page);
                                }
                            });
                        } else {
                            // 没有数据
                            // 清空分页导航
                            $('tbody').empty()
                            $('#pagination').html('没有找到数据')
                        }

                    }
                }
            })
        }
        loadData(1)

        //筛选
        $('#btnSearch').on('click', function (e) {
            e.preventDefault()


            loadData(1)
        })

        // 删除功能
        $('tbody').on('click', '.delete', function () {
            let id = $(this).attr('data-id')
            console.log(id);
            $.ajax({
                type: 'post',
                url: BigNew.article_delete,
                data: {
                    id: id
                },
                success: function (data) {
                    console.log(data);
                    if (data.code == 204) {
                        if ($('tbody tr').length > 1 || selPage == 1) {
                            loadData(selPage)
                            console.log($('tbody tr').length);
                        } else {
                            selPage = selPage - 1
                            console.log($('tbody tr').length);
                            loadData(selPage)
                        }
                    }
                }
            })
        })
        // 点击发表文章
        $('#release_btn').on('click', function () {
            parent.$('.level02 li').eq(1).click()
        })


        // 编辑页面
        $('tbody').on('click', '.btn-edit', function () {

        })
    })
</script>

</html>